<template>
  <div
    class="topic_list"
    :style="'padding-top:' + paddingTop + 'px)'"
    ref="wrapper"
  >
    <!-- <div class="m-notice" v-if="notice">
      <span class="txt">{{ "公告：" + notice }}</span>
      <van-icon name="cross" class="close" @click="closeNotice" />
    </div> -->
    <div class="top-discuss" v-if="top.id">
      <div class="head">
        <div class="icon"></div>
        顶置要贴
      </div>
      <div class="content">
        <div class="title" @click="todetail(top)">{{ top.title }}</div>
        <div class="txt" @click="todetail(top)">
          {{ top.content }}
        </div>
        <div class="bottom">
          <div class="tags" @click="todetail(top)" v-if="top.tag">
            <span class="shape">#</span>{{ top.tag }}
          </div>
          <div v-else></div>
          <div class="icons">
            <div class="middle" @click="todetail(top)">
              <van-icon name="chat-o" class="icon" />
              <span class="num">{{ top.comment }}</span>
            </div>
            <div class="right" @click="postCollect">
              <van-icon name="star-o" class="icon" />
              <span class="num">{{ top.collect }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="main-room" ref="pullRefresh">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
          ref="list"
        >
          <template v-for="i in listData">
            <tpm :key="i.id" :i="i" :checkPlay="true" />
            <!-- v-on:click.native="todetail(i)" -->
          </template>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
import tpm from "../components/TopicItem";
export default {
  components: { tpm },
  name: "topic-list",
  props: ["type", "showNotice"],
  data() {
    return {
      listData: [],
      current: 1,
      isLoading: false,
      page: 1,
      loading: false,
      finished: false,
      notice: null,
      noticeClose: false,
      top: {},
      paddingTop: 0,
      scroll: 0
    };
  },
  activated() {
    this.$refs.wrapper.scrollTop = this.scroll;
    this.$refs.wrapper.addEventListener("scroll", this.saveScroll);
  },
  deactivated() {
    this.$refs.wrapper.removeEventListener("scroll", this.saveScroll);
  },
  mounted() {
    this.$refs.wrapper.addEventListener("scroll", this.saveScroll);
  },
  methods: {
    saveScroll() {
      this.scroll = this.$refs.wrapper.scrollTop;
    },
    postCollect() {
      this.$api.PostCollect(this.top.id).then(r => {
        // console.log(r);
        if (r.data.code == 0) {
          if (r.data.info.id) {
            this.$toast("收藏成功~");
          } else {
            this.$toast("已取消收藏~");
          }
        }
      });
      return;
    },
    closeNotice() {
      this.notice = null;
      this.noticeClose = true;
      window.sessionStorage.setItem("noticeClose", true);
    },
    onLoad() {
      this.indexList();
    },
    onRefresh() {
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true
      });
      this.page = 1;
      this.finished = false;
      this.listData = [];
      this.indexList();
    },
    todetail(row) {
      this.$router.push({ name: "detail", params: row });
    },
    indexList() {
      this.$api
        .indexList({
          type: this.type,
          p: this.page++
        })
        .then(r => {
          r.data.info.list.map(e => (e.current = 1));
          this.listData = [...this.listData, ...r.data.info.list];
          this.isLoading = false;
          this.loading = false;
          this.top = r.data.info.top;
          if (r.data.info.list.length == 0) this.finished = true;
          // console.log(r.data.info);
          this.$toast.clear();
          this.notice = r.data.info.notice;
          this.$emit("showNotice", r.data.info.notice);
        });
    }
  },
  created() {
    this.noticeClose = window.sessionStorage.getItem("noticeClose");
    this.paddingTop = window.sessionStorage.getItem("paddingTop");
  }
};
</script>

<style lang="less">
.topic_list {
  height: calc(100vh - 90px);
  min-height: calc(100vh - 90px);
  overflow: scroll;
  -webkit-overflow-scrolling: touch;
}
.m-notice {
  text-align: center;
  position: relative;
  background: #f7f7f8;
  color: #97969c;
  font-size: 18px;
  padding: 5px 10px;

  .txt {
    max-width: 320px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
  }
  .close {
    position: absolute;
    right: 10px;
    top: 9px;
  }
}
.head-height {
  height: 90px;
}
.top-discuss {
  font-size: 20px;
  .head {
    margin: 5px 13px;
    font-size: 15px;
    line-height: 20px;
    .icon {
      display: inline-block;
      width: 6px;
      height: 18px;
      border-radius: 5px;
      background: #9a98a5;
      vertical-align: middle;
    }
  }
  .content {
    box-shadow: 0 0 12px 1px rgba(0, 0, 0, 0.1);
    // border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    width: 310px;
    // height: 100px;
    margin: auto;
    padding: 10px 20px;
    .title {
      font-weight: bold;
      font-size: 16px;
      letter-spacing: 2px;
    }
    .txt {
      font-size: 14px;
      text-align: justify;
      letter-spacing: 1px;
      margin: 10px 0 5px 0;
      height: 40px;
      line-height: 20px;
      text-indent: 24px;
      overflow: hidden;
      text-overflow: ellipsis;
      text-align: justify;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      .tags {
        padding: 0 5px;
        font-size: 12px;
        padding-top: 2px;
        line-height: 15px;
        max-height: 16px;
        color: #39a9ed !important;
        border-radius: 10px;
        background: #eee;
        display: inline-block;
        .shape {
          font-size: 10px;
          color: #fff;
          background: #39a9ed;
          border-radius: 50%;
          display: inline-block;
          width: 14px;
          height: 14px;
          text-align: center;
          margin-right: 2px;
        }
      }
      .icons {
        width: 100px;
        display: inline-flex;
        line-height: 18px;
        .num {
          display: inline-block;
          font-size: 16px;
        }
        .icon {
          font-size: 18px;
          margin-right: 2px;
        }
        .middle {
          width: 50px;
        }
      }
    }
  }
}
.top-head {
  position: fixed;
  top: 0;
  width: 100%;
  background: #fff;
  z-index: 999999;
  a {
    color: #e8575d;
  }
  .m-menu {
    .tabs {
      display: inline-block;
      width: 375px;
      .van-tab {
        flex-basis: 20% !important;
      }
      .van-tabs__wrap {
        border-width: 0 !important;
      }
      .van-tabs__wrap::after {
        border-width: 0 !important;
      }
      .van-tab--active {
        color: #e8575d !important;
        font-weight: bold;
        font-size: 18px;
        transition: 50ms;
      }
      .van-ellipsis {
        overflow: visible !important;
      }
      .van-tabs__line {
        background-color: #e8575d;
      }
    }
    .btn {
      display: inline-block;
      color: #555;
      padding: 7px 0;
      margin-left: 10px;
      font-size: 22px;
      text-align: center;
      vertical-align: top;
    }
  }
  .search {
    padding: 0;
    display: flex;
    text-align: center;
    line-height: 36px;
    font-size: 26px;
    padding-top: 5px;
    .van-search {
      padding: 0;
    }
    .bell {
      width: 40px;
      color: #e8575d;
    }
    .ipt {
      width: 300px;
    }
    .add {
      width: 40px;
    }
  }
}
</style>
